package com.letoken.platform.admin.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.letoken.platform.pub.res.ApiResponse;
import com.letoken.platform.repo.admin.req.user.TeamPerformancePageReq;
import com.letoken.platform.repo.admin.res.user.TeamPerformancePageRes;
import com.letoken.platform.repo.app.dao.TeamPerformanceMapper;
import com.letoken.platform.repo.utlis.PageDataUtil;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RequestMapping("/team/performance")
@RestController
public class TeamPerformanceController {

    @Resource
    private TeamPerformanceMapper teamPerformanceMapper;

    @PostMapping("/page")
    public ApiResponse<?> list(@RequestBody TeamPerformancePageReq req) {
        Page<TeamPerformancePageRes> page = new Page<>(req.getPageReq().getPage(), req.getPageReq().getSize());
        teamPerformanceMapper.page(page, req.getUserId(), req.getStartTime(), req.getEndTime());

        if (!CollectionUtils.isEmpty(page.getRecords()) && (null!= req.getStartTime() || null != req.getEndTime())) {
            page.getRecords().forEach(
                    r -> {
                        r.setStartTime(req.getStartTime());
                        r.setEndTime(req.getEndTime());
                    }
            );
        }

        return ApiResponse.create(PageDataUtil.getPageData(page));
    }
}
